package br.sed.theenemy.presentation.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import br.sed.theenemy.business.exception.DBException;
import br.sed.theenemy.business.services.Services;
import br.sed.theenemy.model.bean.User;
import br.sed.theenemy.presentation.EnemyErrorConstants;
import br.sed.theenemy.presentation.UserSession;
import br.sed.theenemy.presentation.action.json.JSONBaseAction;
import br.sed.theenemy.presentation.action.json.exception.ExpectedException;

/**
 * 
 * @author JulioCB
 * @email jcbjulio@gmail.com
 * 
 */
public class CountContactsAction extends JSONBaseAction {

	protected Object executeJSON(final ActionMapping mapping,final HttpServletRequest request, final ActionForm form) throws ExpectedException{
		Services services = Services.getInstance();
		
		final UserSession session = new UserSession(request, false);
        final User userLogged = session.getLoggedUser();
        
        if (userLogged == null) {
            // User not logged in
        	throw new ExpectedException(EnemyErrorConstants.USER_NOT_LOGGED);
        } 
		
		Integer userNumber = null;
        try {
        	userNumber = services.countContactsBySourceId(userLogged.getId());
		} catch (DBException e) {
			throw new ExpectedException(EnemyErrorConstants.DB_EXCEPTION);
		}

		return userNumber;
	}
}
